﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FineUI;
using System.Data;
using System.Text;
using ZHDJ.WebPage;
using ZHDJ.Core.Caching;
using ZHDJ.Data.Models.Org;
using AppBox.BLL;
using Newtonsoft.Json;
using ZHDJ.Data;

namespace ZHDJ.Web
{
    public partial class MapOld : System.Web.UI.Page
    {
        public static AppBoxContext DB
        {
            get
            {
                if (!HttpContext.Current.Items.Contains("__AppBoxContext"))
                {
                    HttpContext.Current.Items["__AppBoxContext"] = new AppBoxContext();
                }
                return HttpContext.Current.Items["__AppBoxContext"] as AppBoxContext;
            }
        }



        StringBuilder sbChild = new StringBuilder();
        protected List<PartyOrgBranch> branchList = new List<PartyOrgBranch>();
        protected string json = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindTree();
            }
        }

        /// <summary>
        /// 绑定组织Tree
        /// </summary>
        public void BindTree()
        {
            StringBuilder sb = new StringBuilder();
            TreeControlBusiness tree = new TreeControlBusiness("BranchID", "ParentBranchID", "BranchName",
                true, CacheKeys.Cache_Branch_Tree_Navigation);
            var TreeBranch = new FineUI.Tree();
            tree.BindTreeBranch(TreeBranch);

            foreach (var item in TreeBranch.Nodes)
            {
                var data = new TreeData() { id = item.NodeID, name = item.Text, level = 0 };
                sb.Append("<li><span class=\"folder\">" + data.name + "</span>");

                BindTreeChild(item.Nodes, data);

                sb.Append(sbChild.ToString());
                sb.Append("</li>");
            }
            liTree.Text = sb.ToString();
            json = JsonConvert.SerializeObject(branchList);
        }

        private void BindTreeChild(FineUI.TreeNodeCollection treeNodes, TreeData treeData)
        {
            if (treeNodes == null)
                return;

            foreach (var item in treeNodes)
            {
                sbChild.Append("<ul>");
                var data = new TreeData() { id = item.NodeID, name = item.Text, level = treeData.level + 1 };

                if (item.Nodes.Count == 0)
                {
                    var model = DB.PartyOrgBranchs.Find(data.id);
                    branchList.Add(model);

                    sbChild.Append("<li><span class=\"file\" onclick=\"clickItem(" + model.XPoint + "," + model.YPoint + ")\" >" + data.name + "</span>");
                }
                else
                    sbChild.Append("<li><span class=\"folder\">" + data.name + "</span>");

                BindTreeChild(item.Nodes, data);
                sbChild.Append("</li>");
                sbChild.Append("</ul>");
            }
        }

    }

    public class TreeData
    {
        public TreeData()
        {
            id = "";
            name = "";
            children = new List<TreeData>();
            level = 0;
        }

        public int level { get; set; }
        public string id { get; set; }
        public string name { get; set; }
        public List<TreeData> children { get; set; }
    }
}